package com.zyw.zhuangdian.controller;


import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.enums.MsgCode;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.ArticleDtoIn;
import com.zyw.zhuangdian.pojo.in.ProductionDtoIn;
import com.zyw.zhuangdian.pojo.out.ContentDtoOut;
import com.zyw.zhuangdian.service.ArticleService;
import com.zyw.zhuangdian.service.ProductionService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/content")
@RequiredArgsConstructor
@Slf4j
public class ContentController {
    private final ProductionService productionService;
    private final ArticleService articleService;

    /**
     * 搜索内容
     * @param json
     * @return
     */
    @RequestMapping(value = "search",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> search(@RequestBody JSONObject json){
        Assert.hasKey(json,"keyword","搜索内容不能为空");
        Assert.hasKeyAndValue(json,"mode","搜索模式不能为空");

        ArticleDtoIn articleDtoIn = BeanConvertUtil.covertBean(json,ArticleDtoIn.class);
        ProductionDtoIn productionDtoIn = BeanConvertUtil.covertBean(json,ProductionDtoIn.class);

        Resp<ContentDtoOut> resp = new Resp<>();
        ContentDtoOut contentDtoOut = new ContentDtoOut();

        if (ServiceConstant.PRODUCTION.equals(json.getString("mode"))){
            // 仅查询产品
            productionDtoIn.setTitle(json.getString("keyword"));
            contentDtoOut.setProductions(productionService.listProductions(productionDtoIn));
        }else if (ServiceConstant.ARTICLE.equals(json.getString("mode"))){
            // 仅查询文章
            articleDtoIn.setTitle(json.getString("keyword"));
            contentDtoOut.setArticles(articleService.listArticles(articleDtoIn));
        }else if (ServiceConstant.ALL.equals(json.getString("mode"))){
            // 两者都查
            productionDtoIn.setTitle(json.getString("keyword"));
            articleDtoIn.setTitle(json.getString("keyword"));
            contentDtoOut.setProductions(productionService.listProductions(productionDtoIn));
            contentDtoOut.setArticles(articleService.listArticles(articleDtoIn));
        }else{
            // 直接返回失败
            return RespUtil.SYSTEM_ERROR(MsgCode.MSG_ILLEGAL_ARGUMENT);
        }

        resp.setData(contentDtoOut);
        return RespUtil.OK(resp);
    }

    /**
     * 推荐内容
     * @param json
     * @return
     */
    @RequestMapping(value = "recommend",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> recommend(@RequestBody JSONObject json){

        return RespUtil.OK(MsgCode.MSG_SUCCESS);
    }
}
